Page({
    data: {
      socketOpen: false
    },
  
    onLoad: function(options) {
      // 在页面加载时检查 WebSocket 是否已连接
      this.checkSocketConnection();
    },
  
    checkSocketConnection: function() {
      const that = this;
      wx.onSocketOpen(() => {
        console.log('WebSocket 已连接');
        that.setData({
          socketOpen: true
        });
      });
  
      wx.onSocketError((err) => {
        console.error('WebSocket 错误：', err);
        wx.showToast({
          title: '连接失败，请重试',
          icon: 'none'
        });
      });
    },
  
    sendCommand: function(event) {
      const command = event.currentTarget.dataset.command;
      console.log('发送指令:', command);
  
      if (this.data.socketOpen) {
        wx.sendSocketMessage({
          data: command
        });
      } else {
        wx.showToast({
          title: 'WebSocket 未连接',
          icon: 'none'
        });
      }
    },
    // 检测按钮的处理逻辑
    detect: function() {
     const { deviceId, serviceId, characteristicId } = this.data;

        wx.navigateTo({
         url: `/pages/result/result?deviceId=${deviceId}&serviceId=${serviceId}&characteristicId=${characteristicId}`
        })
    }
  });
  